package com.aistar.controller;

import com.aistar.model.bo.Result;
import com.aistar.model.dto.employer.EmployerAndUserDTO;
import com.aistar.model.dto.employer.TagAndTypeDTO;
import com.aistar.model.entity.*;
import com.aistar.service.employer.IEmployerCenterService;
import com.aistar.service.employer.IEmployerRecruitmentService;
import com.aistar.util.BaiduAddressUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: EmployerController
 * @author: LiuWenXiu
 * @date: 2021/9/24 17:37
 * @Description: TODO...本类用来演示:招聘者控制层
 */

@Controller
@RequestMapping("/employer")
public class EmployerController {

    @Autowired
    private IEmployerCenterService employerCenterService;

    @Autowired
    private IEmployerRecruitmentService employerRecruitmentService;

    @Autowired
    private RestTemplate restTemplate;


    // 公共导航栏
//    @GetMapping("/comnav")
//    public String nav(HttpServletRequest request) {
//        // 获取用户的ip
////        String ip = request.getRemoteHost();
////        System.out.println("ip"+ip);
//
////        JSONObject resultJson = readJsonFromUrl()
//
////        String ip = "58.210.179.66";
////        String ak = "BVkHiRTylGewUqLoVTVR6fcAqjSEwvD6";
////        String url = "https://api.map.baidu.com/location/ip?ak="+ak+"&ip="+ip+"&coor=bd09ll";
////        ResponseEntity<Map> forEntity = null;
////        try {
////            forEntity = restTemplate.getForEntity(new URI(url), Map.class);
////            Map body = forEntity.getBody();
////            System.out.println("body"+body);
////            System.out.println(body.values());
////            System.out.println(body.entrySet());
////            System.out.println(body.get("content"));
////        } catch (URISyntaxException e) {
////            e.printStackTrace();
////        }
//
////        BaiduAddressUtil addressUtil = new BaiduAddressUtil();
////        String address = addressUtil.getAddress(ip);
////        request.setAttribute("address",address);
//        HttpSession session = request.getSession();
//        Employee employee = (Employee) session.getAttribute("employee");
//        request.setAttribute("employee", employee);
//        return "/WEB-INF/common/mynav.jsp";
//    }

    // 导航栏
    @GetMapping("/nav")
    public String temp() {
        return "/WEB-INF/employer/employer_nav.jsp";
    }

    // 未处理简历
    @GetMapping("/unprocessed")
    public String unprocessed() {
        return "/WEB-INF/employer/resume/unprocessed_resume.jsp";
    }

    // 已处理简历
    @GetMapping("/processed")
    public String processed() {
        return "/WEB-INF/employer/resume/processed_resume.jsp";
    }

    // 简历广场
    @GetMapping("/resume")
    public String resume() {
        return "/WEB-INF/employer/resume/all_resume.jsp";
    }

    // 我的 - 主页
    @GetMapping("/index")
    public String index(HttpServletRequest request) {
        // 在登录时在session存储employer
        // 首先假设登录的招聘者id是9
//        Integer id = 10;
        HttpSession session = request.getSession();
        Employer employer = (Employer) session.getAttribute("employer");
        // 加载招聘者信息
//        EmployerAndUserDTO employerAndUserDTO = employerCenterService.loadMyInfo(employer.getId());
//        session.setAttribute("employer",employerAndUserDTO);
        return "/WEB-INF/employer/recruitment/post_recruitment.jsp";
    }

    // 我的发布
    @GetMapping("/post")
    public String post() {
        return "/WEB-INF/employer/recruitment/post_recruitment.jsp";
    }

    // 查看职位
    @GetMapping("/view")
    public String view(HttpServletRequest request, Integer id) {
        // 从职位列表页面通过点击查看按钮来到职位的查看页面,传递职位id过来
        Recruitment recruitment = employerRecruitmentService.getRecruitmentById(id);
        request.setAttribute("recruitment", recruitment);
        List<Tag> tagList = employerRecruitmentService.loadTagByRecruitmentId(id);
        request.setAttribute("tagList", tagList);
        return "/WEB-INF/employer/recruitment/view_recruitment.jsp";
    }

    // 修改职位
    @GetMapping("/edit")
    public String edit(HttpServletRequest request, Integer id) {
        Recruitment recruitment = employerRecruitmentService.getRecruitmentById(id);
        request.setAttribute("recruitment", recruitment);
        List<TagAndTypeDTO> tagAndTypeDTOList = employerRecruitmentService.loadTagByType();
        request.setAttribute("tag", tagAndTypeDTOList);
        // 获取该职位的标签id
        List<Tag> tagList = employerRecruitmentService.loadTagByRecruitmentId(id);
        request.setAttribute("tagList", tagList);
        return "/WEB-INF/employer/recruitment/edit_recruitment.jsp";
    }

    // 发布新职位
    @GetMapping("/create")
    public String create(HttpServletRequest request) {
        List<TagAndTypeDTO> tagAndTypeDTOList = employerRecruitmentService.loadTagByType();
        request.setAttribute("tag", tagAndTypeDTOList);
        return "/WEB-INF/employer/recruitment/create_recruitment.jsp";
    }

    // 个人中心
    @GetMapping("/center")
    public String center(HttpServletRequest request) {

        HttpSession session = request.getSession();
        // 加载招聘者信息
        Employer employer = (Employer) session.getAttribute("employer");
        EmployerAndUserDTO employerAndUserDTO = employerCenterService.loadMyInfo(employer.getId());
//        EmployerAndUserDTO employerAndUserDTO = (EmployerAndUserDTO) session.getAttribute("employer");
        request.setAttribute("employer", employerAndUserDTO);

        // 加载公司信息 -
        Company company = employerCenterService.loadCompanyInfo(employerAndUserDTO.getEmployer().getCompanyId());
        if (null != company) {
            request.setAttribute("company", company);
        }
        return "/WEB-INF/employer/center/employer_center.jsp";
    }


    // 我的邀约
    @GetMapping("/invitation")
    public String invitation() {
        return "/WEB-INF/employer/recruitment/invitation.jsp";
    }


    // 按职位查看简历
    @GetMapping("/recruitment/resume")
    public String recruitmentResume(HttpServletRequest request, Integer id) {
        String name = employerRecruitmentService.getRecruitmentById(id).getName();
        request.setAttribute("name", name);
        request.setAttribute("id", id);
        return "/WEB-INF/employer/resume/recruitment_resume.jsp";
    }

}
